<template>
  <div></div>
</template>
<script>
import { getList } from "network/mock.js";

// 引入 echarts 核心模块，核心模块提供了 echarts 使用必须要的接口。
import * as echarts from "echarts/core";
// 引入柱状图图表，图表后缀都为 Chart
import { BarChart, LineChart, PieChart } from "echarts/charts";
// 引入提示框，标题，直角坐标系组件，组件后缀都为 Component
import {
  TitleComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
  ToolboxComponent,
} from "echarts/components";
// 引入 Canvas 渲染器，注意引入 CanvasRenderer 或者 SVGRenderer 是必须的一步
import { CanvasRenderer } from "echarts/renderers";

// 注册必须的组件
echarts.use([
  TitleComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
  ToolboxComponent,
  BarChart,
  LineChart,
  PieChart,
  CanvasRenderer,
]);
export default {
  props: ["bar"],
  data() {
    return {
      chart: null,
      barC: null,
      res: null,
    };
  },
  watch: {
    bar: function(newValue) {
      this.barC = newValue;
      newValue &&
        this.getBarChart(() => {
          this.init();
        });
    },
  },
  mounted() {},
  methods: {
    // 请求数据
    async getBarChart(callback) {
      let { list } = await getList();
      this.res = list.find((r) => r.type === "RightMidEchart");
      console.log(this.res);
      callback && callback();
    },

    // 初始化
    init() {
      // 基于准备好的dom，初始化echarts实例
      var myChart = echarts.init(this.barC);
      // 声明颜色数组
      myChart.setOption({
        // color: ["#065aab", "#066eab", "#0682ab", "#0696ab", "#06a0ab"],
        // 饼形图默认居中
        tooltip: {
          trigger: "item",
        },
        legend: {
          // 距离底部为0%
          bottom: "0%",
          // 小图标的宽度和高度
          itemHeight: 10,
          itemWidth: 10,
          left: "center",
          textStyle: {
            color: "rgba(255,255,255,.5)",
            fontSize: "12",
          },
        },
        series: [
          {
            name: "年龄分布",
            type: "pie",
            // 圆的半径  内圆和外圆
            radius: ["40%", "60%"],
            center: ["50%", "50%"],
            bottom: "10%",
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 10,
              borderColor: "rgba(255,255,255,.5)",
              borderWidth: 1,
            },
            // 中间是否显示文字
            label: {
              show: true,
              color: "rgba(255,255,255,.5)",
              position: "center",
            },
            // 链接图形的线是否显示
            labelLine: {
              show: false,
            },
            data: [
              { value: 1, name: "0岁以下" },
              { value: 4, name: "20-29岁" },
              { value: 2, name: "30-39岁" },
              { value: 2, name: "40-49岁" },
              { value: 1, name: "50岁以上" },
            ],
          },
        ],
      });
      // 设置图表的自动适应
      window.addEventListener("resize", function() {
        myChart.resize();
      });
    },
  },
};
</script>
<style scoped lang="scss"></style>
